//
//  MainController.swift
//  bboySinaWeibo
//
//  Created by apple on 15/11/11.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainController: UITabBarController {
    
    //撰写按钮的点击事件
    func editBtnClick() {
        
        print("点击我进行编辑")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = MainTabBar()
        
        //由于 tabBar 是只读的 所以用KVC赋值
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()
        
        //设置撰写按钮的点击事件
        mainTabBar.editBtn.addTarget(self, action: "editBtnClick", forControlEvents: .TouchUpInside)
    }
    
    /**
    加载子控制器
    */
    func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(DiscoveryTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(ProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
    }
    
    //重载
    private func addChildViewController(vc: UIViewController,imageName:String,title:String) {
        
        //给每个控制器包装一个导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置高亮图片是原始大小
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        addChildViewController(nav)
    }
}
